﻿/********************************************
* 公司名称：snail information
* 创 建 人：cfhknight
* 创建日期：2025/9/29 13:13:49
* 说    明：Number extensions 
* 版    本：V0.0.0.1
********************************************/
namespace SnailInfo.ConvertExtension
{
    public static class NumberExtension
    {
        /// <summary>
        /// Convert string to int
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentException"></exception>
        public static int ToInt(this string value)
        {
            if (string.IsNullOrEmpty(value)) return 0;
            if (int.TryParse(value.Trim(), out int result)) {
                return result;
            }
            throw new ArgumentException($"the value of {value} can not be convert to int！");

        }
        /// <summary>
        /// Convert string to long
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentException"></exception>
        public static long ToLong(this string value)
        {
            if (string.IsNullOrEmpty(value)) return 0;
            if (long.TryParse(value.Trim(), out long result)) {
                return result;
            }
            throw new ArgumentException($"the value of {value} can not be convert to long ！");
        }
        /// <summary>
        /// convert string to decimal
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentException"></exception>
        public static decimal ToDecimal(this string value)
        {
            if (string.IsNullOrEmpty(value)) { return 0; }
            if (decimal.TryParse(value.Trim(), out decimal result)) {
                return result;
            }
            throw new ArgumentException($"the value of {value} can not be convert to decimal ！");
        }
        /// <summary>
        /// convert string to decimal with precision
        /// </summary>
        /// <param name="value"></param>
        /// <param name="precision"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentException"></exception>
        public static decimal ToDecimal(this string value, int precision)
        {
            if (string.IsNullOrEmpty(value)) { return 0; }
            if (decimal.TryParse(value.Trim(), out decimal result)) {

                return Math.Round(result, precision, MidpointRounding.AwayFromZero);
            }
            throw new ArgumentException($"the value of {value} can not be convert to decimal ！");
        }
        /// <summary>
        /// convert to decimal with precision
        /// </summary>
        /// <param name="value"></param>
        /// <param name="precision"></param>
        /// <returns></returns>
        public static decimal ToDecimal(this decimal value, int precision)
        {

            return Math.Round(value, precision, MidpointRounding.AwayFromZero);
        }
        /// <summary>
        /// 两个decimal相除后向下取整
        /// </summary>
        /// <param name="Dividend">被除数</param>
        /// <param name="divisor">除数</param>
        /// <returns></returns>
        public static decimal RoundDown(this decimal Dividend, decimal divisor)
        {
            if (divisor == 0) {
                throw new ArgumentException($"the number of divisor can not be zero");
            }
            return Math.Floor(Dividend / divisor);
        }
        /// <summary>
        /// 计算两个decimal相除后向上取整
        /// </summary>
        /// <param name="sDividend">被除数</param>
        /// <param name="divisor">除数</param>
        /// <returns></returns>
        public static decimal RoundUp(this decimal sDividend, decimal divisor)
        {
            if (divisor == 0) {
                throw new ArgumentException($"the number of divisor can not be zero");
            }
            var value = Math.Ceiling(sDividend / divisor);
            return value;
        }
    }
}
